<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\Overgroup;
use app\admin\model\Item;
use app\admin\model\OvergroupItem;

class OvergroupController extends CommonController {
    /*
     * 超值组合列表
     * author xfwed 
     * date 2019.09.10
     */

    public function overGroupList() {
        $name = input('param.name');//名称
        $type = input('param.type');//全部传null 0.普通组合 1.美修斯
        $is_special = input('param.is_special');//是否特色 1.是 2.否  0全部
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if($name){
            $where['name'] = ['like','%'.$name.'%'];
        }
        if(is_numeric($type)){
            $where['type'] = $type;
        }
        if($is_special){
            $where['is_special'] = $is_special;
        }
        $where = isset($where)?$where:[];
        $overGroupList = Overgroup::order('status asc,id desc')->where($where)->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $overGroupList);
    }
    
    /*
     * 超值组合项目列表
     * author xfwed 
     * date 2019.09.10
     */

    public function overGroupItemList() {
        $id = input('param.id');
        //$item_ids = OvergroupItem::where('overgroup_id','=',$id)->column('item_id');
        $overgroup = Overgroup::where('id',$id)->find();
        $itemList = OvergroupItem::alias('ogi')
                ->join('item i',  'i.id = ogi.item_id','left')
                ->join('item_class c', 'i.item_type = c.id', 'LEFT')
                ->join('item_class c2', 'i.type = c2.id', 'LEFT')
                ->field('i.id,i.item_name,i.item_img,i.item_price,i.special_money,i.is_del,i.add_time,i.visit_count,i.sale_money,c.name item_type,c2.name type')
                ->where('ogi.overgroup_id',$id)
                ->select();
        
        $data['overgroup'] = $overgroup;
        $data['itemList'] = $itemList;
        return $this->ajaxReturn(0, '查询成功', $data);
        
    }
    
    /*
     * 添加超值组合选择项目列表
     * author xfwed 
     * date 2019.09.12
     */

    public function itemList() {
        $itemList = Item::field('id,item_name')
                ->where('is_del','=',1)
                ->select();
        return $this->ajaxReturn(0, '查询成功', $itemList);
        
    }
    
    /*
     * 添加超值组合
     * author xfwed 
     * date 2019.09.10
     */

    public function addOverGroup() {
        $rule = [
            'name' => 'require',
            'price' => 'require',
            'img' => 'require',
            'info_img' => 'require',
            'index_img' => 'require',
            'type' => 'require',
            'special_money' => 'require',
        ];
        $msg = [
            'name.require' => '名称必须',
            'price.require' => '组合价必须',
            'img.require' => '列表图必须',
            'info_img.require' => '详情图必须',
            'index_img.require' => '首页列表图必须',
            'type.require' => '组合类型必须',
            'special_money.require' => '特权价必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        $itemJson = $all['itemJson'];//示例  [{"itemId":"1","num":3},{"itemId":"2","num":2}]
        $a = json_decode($itemJson,TRUE);
        $itemIds = [];
        $full_price = 0;
        foreach ($a as $k => $v){
            for($i = 0 ;$i<$v['num'];$i++){
                $itemIds[] = $v['itemId'];
            }
            $item_price = Item::where('id',$v['itemId'])->value('item_price');
            $full_price = $full_price + $item_price*$v['num'];
        }
        $goodsJson = input('param.goodsJson');//普通超值组合赠品 [{"goods_id":"1","number":3},{"goods_id":"2","number":2}]
        $goodsIds = json_decode($goodsJson,TRUE);
        //$itemIds = $all['itemIds']; //项目ids
        //$full_price = Item::where('id','in',$itemIds)->sum('item_price');
        //$itemIds = explode(",", $itemIds);
        $type = $all['type'];//	0.普通组合 1.美修斯
        $platform_rate = input('param.platform_rate');//平台分佣
        $factory_rate = input('param.factory_rate');//工厂分佣
        $store_rate = input('param.store_rate');//店铺分佣
        $staff_rate = input('param.staff_rate');//美容师分佣
        if($type == 1 ){
            $rateNum = $platform_rate + $factory_rate + $store_rate + $staff_rate;
            if($rateNum != 100){
                return $this->ajaxReturn(1, '分佣比例有误', null);
            }
            
        }

        Db::startTrans();
        $data['name'] = $all['name'];
        $data['type'] = $type;
        $data['price'] = $all['price'];
        $data['full_price'] = $full_price;
        $data['special_money'] = $all['special_money'];
        $data['img'] = $all['img'];
        $data['info_img'] = $all['info_img'];
        $data['index_img'] = $all['index_img'];
        $data['note'] = input('param.note');
        $data['add_time'] = date('Y-m-d H:i:s');
        $data['platform_rate'] = $platform_rate;
        $data['factory_rate'] = $factory_rate;
        $data['store_rate'] = $store_rate;
        $data['staff_rate'] = $staff_rate;
        $insertId = Overgroup::insertGetId($data);
        $itemData = [];
        foreach ($itemIds as $key => $value) {
            $oneData = [
                'overgroup_id' => $insertId,
                'item_id' => $value,
            ];

            array_push($itemData, $oneData);
        }
        $imgResult = OvergroupItem::insertAll($itemData);
        
        $goodsData = [];
        if($goodsIds){
            foreach ($goodsIds as $key => $val){
                $oneData = [
                    'overgroup_id' => $insertId,
                    'goods_id' => $val['goods_id'],
                    'number' => $val['number'],
                    'note' => $val['note'],
                    'add_time' => date('Y-m-d H:i:s'),
                ];

                array_push($goodsData, $oneData);
            }
            $goodsResult = Db::name('overgroup_goods')->insertAll($goodsData);
        }  else {
            $goodsResult = TRUE;
        }
        
        if ($insertId && $imgResult && $goodsResult) {
            Db::commit();
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            Db::rollback();
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }
    
    /*
     * 删除超值组合
     * author xfwed 
     * date 2019.09.10
     */

    public function delOverGroup() {
        $id = input('param.id');
        $data['status'] = 2;
        $data['end_time'] = date('Y-m-d H:i:s');
        $add = Overgroup::where('id', '=', $id)->update($data);
        if ($add) {
            return $this->ajaxReturn(0, '下架成功', null);
        } else {
            return $this->ajaxReturn(3, '下架失败', null);
        }
    }
    
    /*
     * 设置/取消特色超值组合
     * author xfwed
     * date 2021-06-23
     */

    public function setOrCallOffSpecial() {
        $id = input('param.id');//超值组合id
        $is_special = input('param.is_special');//是否特色1.是2.否
        $row = Overgroup::where('id',$id)
                ->find();
        if($row['type'] == 1){
            return $this->ajaxReturn(3, '美修斯不能设置为特色', null);
        }
        $save = Overgroup::where('id',$id)->update(['is_special'=>$is_special]);
        if($save){
            return $this->ajaxReturn(0, '操作成功', null);
        }  else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }
    
    
    
    

}
